library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(forecast)
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ################################### WARNING ###################################
## # We noticed you have dplyr installed. The dplyr lag() function breaks how    #
## # base R's lag() function is supposed to work, which breaks lag(my_xts).      #
## #                                                                             #
## # If you call library(dplyr) later in this session, then calls to lag(my_xts) #
## # that you enter or source() into this session won't work correctly.          #
## #                                                                             #
## # All package code is unaffected because it is protected by the R namespace   #
## # mechanism.                                                                  #
## #                                                                             #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## # You can use stats::lag() to make sure you're not using dplyr::lag(), or you #
## # can add conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop   #
## # dplyr from breaking base R's lag() function.                                #
## ################################### WARNING ###################################
library(tsibble)
## 
## Attaching package: 'tsibble'
## The following object is masked from 'package:zoo':
## 
##     index
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
library(ggplot2)
library(fpp3)
## ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
## ✔ tibble      3.2.1     ✔ tsibbledata 0.4.1
## ✔ dplyr       1.1.1     ✔ feasts      0.3.1
## ✔ tidyr       1.3.0     ✔ fable       0.3.3
## ✔ lubridate   1.9.2     ✔ fabletools  0.3.2
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date()     masks base::date()
## ✖ dplyr::filter()       masks stats::filter()
## ✖ dplyr::first()        masks xts::first()
## ✖ tsibble::index()      masks zoo::index()
## ✖ tsibble::intersect()  masks base::intersect()
## ✖ lubridate::interval() masks tsibble::interval()
## ✖ dplyr::lag()          masks stats::lag()
## ✖ dplyr::last()         masks xts::last()
## ✖ tsibble::setdiff()    masks base::setdiff()
## ✖ tsibble::union()      masks base::union()
library(readxl)
library(TSstudio)
library(timetk)
setwd("C:/Users/fabia/OneDrive/Documentos/UNAL/Series de tiempo/Parcial final")
tasa_desempleo <- read_excel("DesempleoUK.xlsx",sheet = "TotalUK")
ts_desempleo<-ts(rev(tasa_desempleo$Tasa_Desempleo), start = c(1993,01), frequency = 12, end = c(2019,12))
xts_desempleo = as.xts(ts_desempleo)
plot(xts_desempleo, main = "Tasa de desempleo",xlab="Tiempo")

acf(ts_desempleo,main="",sub="Figura 1: Función de Autocorrelación Simple")

pacf(ts_desempleo,main="",sub="Figura 1: Función de Autocorrelación Parcial")

### Estabilización de la varianza marginal

lambda=forecast::BoxCox.lambda(ts_desempleo, method = "guerrero", lower = -1, upper = 3)
lambda
## [1] -0.2154912
ts_desempleo_One=forecast::BoxCox(ts_desempleo,lambda=lambda)
plot(forecast::BoxCox(ts_desempleo_One,lambda=lambda))

### Eliminación de la tendencia

ndiffs(ts_desempleo_One) # numero de diferenciaciones
## [1] 1
dts_desempleo<-diff(ts_desempleo_One) # 
d_xts_desempleo = as.xts(dts_desempleo) #queda esta >> dts_desempleo 
ndiffs(dts_desempleo)  # numero de diferenciaciones
## [1] 0
plot(d_xts_desempleo, main = "Tasa desempleo diferenciada",xlab="Tiempo")

acf(dts_desempleo, lag.max = 50)

pacf(dts_desempleo, lag.max = 50)

### Detección de ciclos

tbl_ts_desempleo<- as_tsibble(dts_desempleo)
tbl_ts_desempleo%>%select(value)%>%gg_season(period = "year")
## Plot variable not specified, automatically selected `y = value`

tbl_ts_desempleo%>%select(value)%>%gg_subseries(period = "year")
## Plot variable not specified, automatically selected `y = value`

tbl_desempleo<-as_tibble(tbl_ts_desempleo)
tbl_desempleo$index<-as.Date(tbl_desempleo$index)
tbl_desempleo
## # A tibble: 323 × 2
##    index         value
##    <date>        <dbl>
##  1 1993-02-01  0.0404 
##  2 1993-03-01  0.0172 
##  3 1993-04-01  0.0147 
##  4 1993-05-01  0.00259
##  5 1993-06-01 -0.0299 
##  6 1993-07-01 -0.0120 
##  7 1993-08-01 -0.0403 
##  8 1993-09-01  0.0189 
##  9 1993-10-01  0.0190 
## 10 1993-11-01  0.0111 
## # ℹ 313 more rows
tbl_desempleo%>%plot_seasonal_diagnostics(.date_var = index,.value = value,.feature_set = c("month.lbl"),.geom="boxplot") 

Periodograma

spectrum(tbl_desempleo$value,log='no')
abline(v=1/12, lty=2,col="red")
abline(v=2/12, lty=2,col="blue") ### periodo cada 6 meses
abline(v=3/12, lty=2,col=4)
abline(v=4/12, lty=2,col=7)